package cn.diancan.action.manager;

import java.util.List;

import cn.diancan.action.BaseAction;
import cn.diancan.exception.OutMsgException;
import cn.diancan.manager.security.ManagerAuth;
import cn.diancan.model.Specification;
import cn.diancan.model.SpecificationValue;
import cn.diancan.vo.manager.SpecificationManagerActionVo;
import cn.diancan.vo.other.Pager;

import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ModelDriven;

public class SpecificationAction extends BaseAction implements ModelDriven<SpecificationManagerActionVo>{

    private SpecificationManagerActionVo requestVo=new SpecificationManagerActionVo();
    
    private Pager pager =new Pager();
    
    //List
    @ManagerAuth
    public String specificationListPage(){
	try {
	    pager.setPageNumber(requestVo.getPageNumber());
	    pager.setPageSize(10);
	    specificationService.findPagerBySpecification(pager,requestVo.getKeyWords());
	    this.put("pager", pager);
	    this.put("requestVo", requestVo);

	    return freemarkView("/views/ftl/manager/specification/specification_list.ftl");
	} catch (OutMsgException e) { 
	    return managerError(e.getMessage());
	}
    }
    
    @ManagerAuth
    public String createSpecificationPage(){ 

	return freemarkView("/views/ftl/manager/specification/create_specification.ftl");
    }
    
    @ManagerAuth
    public String createSpecification(){
	try {
	    if(null== requestVo.getSpecificationAllValue() || "".equals(requestVo.getSpecificationAllValue().trim())){
		return jsonError("请填写规格值");
	    }
	    
	    String[] specificationValues= requestVo.getSpecificationAllValue().split("\n");
	    Specification specification=  specificationService.saveSpecification(requestVo.getSpecificationName(), specificationValues); 
	    JSONObject jsonObject = new JSONObject();
	    jsonObject.put("specificationId",specification.getId());
	    jsonObject.put("specificationName", specification.getName()); 
	    return jsonFormat(jsonObject,"添加成功","添加失败");
	} catch (OutMsgException e) {
	    return jsonError(e);
	}catch (Exception e) {
	    return jsonError("系统繁忙");
	}
    }
  
  @ManagerAuth
  public String specificationInfo(){
	try {
	    Specification specification=specificationService.findSpecificationById(requestVo.getSpecificationId());
	    this.put("specification",specification);
	    String specificationValue =  specification.getValueStr().replaceAll(" ", "\n");
	    this.put("specificationValue",specificationValue);

	    return freemarkView("/views/ftl/manager/specification/specification_info.ftl");
	} catch (OutMsgException e) { 
	    return managerError(e.getMessage());
	}
  }
  
  @ManagerAuth
  public String modifyPlusSpecificationValue(){
	try {
	    String[] specificationValues= new String[]{};
	    if(null!= requestVo.getSpecificationAllValue() && !"".equals(requestVo.getSpecificationAllValue().trim())){
		specificationValues= requestVo.getSpecificationAllValue().split("\n");
	    }
	 
	    Boolean result= specificationService.updatePlusSpecification(requestVo.getSpecificationId(),requestVo.getSpecificationName(), specificationValues); 
	    return jsonFormat(result,"修改成功","修改失败");
	} catch (OutMsgException e) {
	    return jsonError(e);
	}catch (Exception e) {
	    return jsonError("系统繁忙");
	}
  }
  
  @ManagerAuth
  public String specificationValueSearch(){
    try {
	List<SpecificationValue> list =specificationService.findSpecificationValue(requestVo.getSpecificationName(), requestVo.getKeyWords());
	if(null!=list){
	    for (int j = 0; j < list.size(); j++) {
		 SpecificationValue sp= list.get(j);
		 sp.setSpecification(null);
		 sp.setCreateTime(null);
		 sp.setIsDelete(null);
	    }
	   
	}
	return jsonFormat(list);
    } catch (OutMsgException e) {
	return jsonError(e);
    }
  }

    @ManagerAuth
    public String singleSpecificationValueSeacrch() {
	try {
	    SpecificationValue specificationValue = specificationService.findSpecificationValueByNameAndValue(requestVo.getSpecificationName(),
			    requestVo.getKeyWords());
	    return jsonFormat(specificationValue);
	} catch (OutMsgException e) {
	    return jsonError(e);
	}
    }
    @ManagerAuth
    public String sizeSpecificationList(){
	try {
	    List<Specification> list =  specificationService.findSizeSpecificationList();
	    
	    return jsonFormat(list);
	} catch (OutMsgException e) {
	    return jsonError(e);
	}
    }
 
  /* 
    @ManagerAuth
    public String removeSpecification(){
	try {
	    Boolean result =goodsCategoryService.deleteGoodsCategory(requestVo.getGoodsCategoryId());
	    return jsonFormat(result,"删除成功","删除失败");
	} catch (OutMsgException e) { 
	    return jsonError(e);
	}
    }*/
    
    @Override
    public SpecificationManagerActionVo getModel() {
	return requestVo;
    }
    

}
